﻿using System.Collections.Generic;

namespace AuchanScan.Models
{
    internal class ProductDatabase
    {
        #region Singleton

        private static ProductDatabase _instance;

        public static ProductDatabase Instance
        {
            get
            {
                if (_instance == null) 
                {
                    _instance = new ProductDatabase();
                }
                return _instance;
            }
        }

        #endregion

        protected Dictionary<long, Product> Products;

        private ProductDatabase()
        {
            Products = new Dictionary<long, Product>();
          
#if DEBUG
            var tempProducts = new List<Product>();
            tempProducts.Add(new Product {Barcode = 12, Name = "Banane", Price = 12.4});
            tempProducts.Add(new Product {Barcode = 133, Name = "Truc en bois", Price = 32});
            tempProducts.Add(new Product {Barcode = 3583781493874, Name = "sac de couchage", Price = 19.99});
            tempProducts.Add(new Product {Cagnotte = 0.4, Barcode = 5029053033648, Name = "Kleenex", Price = 9.99});
            tempProducts.Add(new Product
                {
                    Id = 1234,
                    Barcode = 3033710076017,
                    Name = "Nescafé Special Filtre",
                    IsTicketRestoCompliant = true,
                    Price = 4.39
                });

            tempProducts.Add(new Product
                {
                    Id = 12345,
                    Barcode = 3175680012937,
                    Name = "Choco Fondant Gerblé",
                    IsTicketRestoCompliant = true,
                    Price = 3.49
                });
            tempProducts.Add(new Product
                {
                    Id = 1234567,
                    Barcode = 9782100515721,
                    Name = "Livre",
                    IsTicketRestoCompliant = false,
                    Price = 2.19
                });

            tempProducts.ForEach((p) => Products.Add(p.Barcode, p));
#endif
        }

        public Product SearchProductFromBarcode(long barcode)
        {
            Product result;
            Products.TryGetValue(barcode, out result);
            return result;
        }


        internal void SetProducts(List<Product> productsToImport)
        {

            Products.Clear();
            productsToImport.ForEach((p) => Products.Add(p.Barcode, p));
        }
    }
}